

var fs = require('fs');
var path = require('path');
var async = require('async');
var glob = require('glob');

// 小程序打包成二进制
var minappack = require('./minapppack.js');

//代码压缩
var uglifyjs = require('uglify-es');
// es6 转 es5 
var babel = require('babel-core');

// css 自动加前缀
var autoprefixer = require('autoprefixer');
var postcss = require('postcss');


// 写入文件
function writeFile(src, data) {
	return new Promise(function(resolve, reject){
		fs.writeFile(src, data, (err) => {
		   if (err) {
		   		reject(err);
		   		return;
		   }
		   resolve(data);// 成功写入文件
		});
	});
}

//
function fileIsJs(file) {
	return path.extname(file) === '.js'; 
} 

function fileIsCss(file) {
	return path.extname(file) === '.wxss'; 
}

// 打包小程序
function pack(src, dest) {
	return transform(src, dest).then(function(){
		return minappack(src, dest);
	});
}

// es6 转 es5 并压缩代码
function transformJs(src) {
	return new Promise(function(resolve, reject){
		// es6 转化 会自动加 "use strict"
		babel.transformFile(src, {presets: 'es2015'}, function (err, babelresult) {
			if (err) {
				reject(err);
				return;
			}
			//压缩js
			var uglifyjsResult = uglifyjs.minify(babelresult.code);
			if (uglifyjsResult.error) {
				reject(uglifyjsResult.error);
				return;
			}
			// 写入js 
			var code = uglifyjsResult.code;
			writeFile(src, code).then(()=>{
				resolve();
			},(error)=>{
				reject(error);
			});
		});
	});
}

// 转化css 自动加前缀
function transformCss(src) {
	return new Promise(function(resolve, reject){
		fs.readFile(src,function(err, css){
			if (err) {
				reject(err);
				return;
			}
			postcss([autoprefixer]).process(css).then(function (result) {
			    return writeFile(src, result.css);
			}).then(()=>{
				resolve();
			})
				.catch(error => {
  				reject(error);
				});
		});
	});
}

//转化 js wxss 压缩转化js wxss 加前缀
function transform(src, desc) {
	return new Promise(function(resolve,reject){
		glob(`${src}/**/*`, function (err, files) {
			if (err) {
				reject(err);
			}
		    async.each(files, function(file, callback){
				if (fileIsJs(file)) {
					transformJs(file).then(function(code){
						callback(null);
					},function(err){
						callback(err);
					});
				} else if (fileIsCss(file)) {
					transformCss(file).then(function(code){
						callback(null);
					},function(err){
						callback(err);
					});
				} else {
					callback(null); //.json .wxml 不处理
				}
				
				
			},function(err){
				if (err) {
					console.log('error data' + err);
					reject(err);
				}
				resolve();
			});
		});
	});
}



function test() {
	var src = path.resolve(__dirname, '../storage/unzip/wxapp/we7/pages/selectarea/selectarea.js');
	var data = fs.readFileSync(src, {encoding :'UTF8'});
	var babelresult = babel.transform(data, {presets: 'es2015'});
	var result = uglifyjs.minify(data);
	console.log('"use strict";'+ result.code);
}

function testcss() {
	var src = path.resolve(__dirname, '../storage/unzip/we7app/app.wxss');
	transformCss(src)
		.then(()=>{
			console.log('success');
		});
}

function testJs() {
	var src = path.resolve(__dirname, '../storage/unzip/we7app/app.js');
	transformJs(src)
		.then(()=>{
			console.log('success');
		});
}

function testfileType() {
	var src = path.resolve(__dirname, '../storage/unzip/we7app/app.js');
	console.log(fileIsJs(src));
}

function testTransform() {
	var basename = 'wxapp';//'1d596380-93c4-11e7-be13-3d91486139a3';
	var src = path.resolve(__dirname,'../storage/unzip/'+basename);
	var dest = path.resolve(__dirname,'../storage/wx/')+basename+'.wx';
	transform(src, dest).then(function(){
		console.log(' transform success');
	});
}

function testminappPack() {
	var basename = 'wxapp';//'1d596380-93c4-11e7-be13-3d91486139a3';
	var src = path.resolve(__dirname,'../storage/unzip/'+basename);
	var dest = path.resolve(__dirname,'../storage/wx/'+basename+'.wx');
	minappack(src, dest).then(()=>{
		console.log('success');
	},(error)=>{
		console.log('error');
	});
}
function testPack() {
	var basename = '0498b690-9449-11e7-8b37-bd2e5a893f54';//'1d596380-93c4-11e7-be13-3d91486139a3';
	var src = path.resolve(__dirname,'../storage/unzip/'+basename);
	var dest = path.resolve(__dirname,'../storage/wx/'+basename+'.wx');
	pack(src, dest).then(()=>{
		console.log('success');
	},(error)=>{
		console.log('error');
	});
}
// testPack(); 
module.exports = pack;


